package cn.xm.canting.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import cn.djp.canting.R;
import cn.xm.canting.bean.Setting;

//用于在设置界面显示设置各项的listview的adapter
public class SettingItemAdapter extends BaseAdapter {

    private List<Setting> objects = new ArrayList<Setting>();

    private Context context;
    private LayoutInflater layoutInflater;

    public SettingItemAdapter(Context context,List<Setting> objects) {
        this.objects = objects;
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public Setting getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.setting_item, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews((Setting)getItem(position), (ViewHolder) convertView.getTag());
        return convertView;
    }

    private void initializeViews(Setting object, ViewHolder holder) {
        holder.name.setText(object.getName());
    }

    protected class ViewHolder {
        private TextView name;
        public ViewHolder(View view) {
            name = view.findViewById(R.id.setting_item);
        }
    }
}
